
#include "stdarg.h"
#include "monitor.h"
#include "common.h"

/* user-mode printf */
int printf( const char *fmt, ... )
{
	va_list args;
	va_start(args, fmt);
	int count;
	char buf[20];
	
	int len = 8;
	
	while ( *fmt != 0 )
	{
		switch ( *fmt ){
			case '%':
				fmt++;
				switch ( *fmt )
				{
				case 'd':
				case 'i':
					sitoa( buf, 10, va_arg( args, int ));
					monitor_write(buf);
					break;
				case 'u':	// Unsigned
					sitoa( buf, 16, va_arg( args, uint_t ) );
					monitor_write(buf);
					break;							
				case 'x':	// hex
					itoa( buf, 16, va_arg( args, uint_t ), len, 'a' );
					monitor_write(buf);
					break;
				case 'X':	// HEX
					itoa( buf, 16, va_arg( args, uint_t), len , 'A' );
					monitor_write(buf);
					break;
				case 'o':	// Octal
					sitoa( buf, 8, va_arg( args, uint_t ), len );
					monitor_write(buf);
					break;
				case 's':	// string (char*)
					monitor_write( va_arg(args, char*) );
					break;
				case 'c':	// char
					monitor_put( va_arg(args, char) );
					break;
							
				}
				fmt++;
				break;
			default:
				monitor_put(*fmt);
				fmt++;
		}
		count++;
	}
	//updatecsr();
	va_end(args);
	return count;
}

